package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.Customer;
import com.bluedot.www.core.pojo.DO.Group;
import com.bluedot.www.core.pojo.DTO.GroupDTO;
import com.bluedot.www.core.utils.DbUtil;
import org.apache.poi.hssf.record.GroupMarkerSubRecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 客户分组，心理咨询师创建的分组信息，只有一级分组，如果分组内存在客户的话则不删除数据 服务类
 * </p>
 *
 * @author zhouxuan
 * @since 2021-08-07
 */
public class GroupService {

    private static final String GROUP_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.GroupMapper.";


    private static final String CUSTOMER_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.CustomerMapper.";


    public static Object execute(String methodName, Object[] param) {
        GroupService groupService = new GroupService();
        Object result = null;
        switch (methodName) {
            case "insertGroup":
                result = groupService.insertGroup((Group) param[0]);
                break;
            case "deleteGroup":
                result = groupService.deleteGroup((Long) param[0]);
                break;
            case "listGroups":
                result = groupService.listGroups((Long) param[0]);
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }

    /**
     * 新建一个分组【新建分组之前，先查询该分组名是否存在，若不存在可以创建】
     *
     * @param group:分组对象
     * @return void
     * @author ZhouXuan
     * @date 2021/8/7 20:24
     */
    private boolean insertGroup(Group group) {
        //新建分组之前，先查询该分组名是否存在
        HashMap<String, String> map1 = new HashMap<>();
        HashMap<String, String> map2 = new HashMap<>();
        map1.put("groupName", group.getGroupName());
        map2.put("userId", group.getUserId().toString());

        List<Group> groups = (List<Group>) Execute.execute(Execute.QUERY_TYPE, GROUP_MAPPER_ADDRESS + "findGroupByName", new Object[]{map1, map2});

        // 如果分组不存在,可以进行创建
        if (groups.size() == 0) {
            // 设置主键
            group.setId(DbUtil.generateId());
            // 设置创建时间以及更新时间
            group.setGmtCreate(DbUtil.getCurrentTime());
            group.setGmtModified(DbUtil.getCurrentTime());
            //添加分组
            Integer count2 = (Integer) Execute.execute(Execute.UPDATE_TYPE, GROUP_MAPPER_ADDRESS + "insertGroup", new Object[]{group});
            return count2 > 0 ? true : false;
        }
        return false;
    }


    /**
     * 删除一个分组【删除分组之前，查询该分组下是否存在客户，若存在，不能进行删除，并提示用户】
     *
     * @param groupId: 分组编号
     * @return boolean
     * @author ZhouXuan
     * @date 2021/8/8 9:54
     */
    private boolean deleteGroup(Long groupId) {
        // 删除分组之前先判断分组内是否还有客户
        List<Customer> customers = (List<Customer>) Execute.execute(Execute.QUERY_TYPE, CUSTOMER_MAPPER_ADDRESS + "findByGroupId", new Object[]{groupId});
        if (customers.size() > 0) {
            // 若有客户，返回false，即如果有客户，则不能删除客户，并告诉前端，组内仍有客户
            return false;
        } else {
            // 若没有客户，进行下一步，删除分组
            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, GROUP_MAPPER_ADDRESS + "deleteGroup", new Object[]{groupId});
            return count > 0 ? true : false;
        }
    }


    /**
     * 查找所有分组
     *
     * @param userId: 用户编号
     * @return java.util.List<com.bluedot.www.core.pojo.DO.Group>
     * @author ZhouXuan
     * @date 2021/8/7 20:52
     */
    private List<GroupDTO> listGroups(Long userId) {
        ArrayList<GroupDTO> groupDTOS = new ArrayList<>();
        GroupDTO groupDTO = null;
        List<Group> groups = (List<Group>) Execute.execute(Execute.QUERY_TYPE, GROUP_MAPPER_ADDRESS + "listGroups", new Object[]{userId});
        for (Group group : groups) {
            groupDTO = new GroupDTO();

            groupDTO.setId(group.getId());
            groupDTO.setGroupName(group.getGroupName());
            groupDTO.setUserId(group.getUserId());
            groupDTO.setGmtCreate(group.getGmtCreate());
            groupDTO.setGmtModified(group.getGmtModified());

            List<Integer> counts = (List<Integer>) Execute.execute(Execute.QUERY_TYPE, GROUP_MAPPER_ADDRESS + "calcCount", new Object[]{group.getId()});

            groupDTO.setCount(counts.get(0));

            groupDTOS.add(groupDTO);
        }
        return groupDTOS;
    }
}
